<template>
	<el-dialog
		:title="dialog.title"
		v-model="dialog.dialogVisible"
		:close-on-click-modal="false"
		:close-on-press-escape="false"
		destroy-on-close
		width="1000px"
		top="0"
	>
		<el-form
			ref="form"
			:model="form"
			:rules="formRules"
			v-loading="formLoading"
			label-width="120px"
		>
			<el-row>
				<el-col :span="12">
					<el-form-item label="活动类型" prop="activity_type">
						<el-radio-group
							v-model="form.activity_type"
						>
							<el-radio :label="1">打折</el-radio>
							<el-radio :label="2">满减</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="排序" prop="sort">
						<el-input-number v-model="form.sort"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="活动名称" prop="activity_name">
						<el-input v-model="form.activity_name"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="列表标签" prop="activity_tag">
						<el-input v-model="form.activity_tag"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="开始时间" prop="start_time">
						<el-date-picker v-model="form.start_time" type="datetime"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="结束时间" prop="end_time">
						<el-date-picker v-model="form.end_time" type="datetime"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="详情说明" prop="remark">
						<el-input v-model="form.remark"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="dialog.dialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="submitData">提 交</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script>
import dayjs from "dayjs";

export default {
	name: "edit",
	data() {
		return {
			dialog: {
				dialogVisible: false,
				title: ""
			},
			formLoading: false,
			defaultForm: {
				activity_type: 2,
				sort: 0,
				activity_name: "",
				activity_tag: "",
				start_time: '',
				end_time: '',
				remark: ""
			},
			form: {
				activity_type: 2,
				sort: 0,
				activity_name: "",
				activity_tag: "",
				start_time: "",
				end_time: "",
				remark: "",
			},
			formRules: {

			}
		}
	},
	emits: ["success"],
	methods: {
		add() {
			this.dialog.title = "新增活动";
			this.dialog.dialogVisible = true;
			this.form = Object.assign({}, this.defaultForm);
		},
		edit(data) {
			this.dialog.title = "编辑活动";
			this.dialog.dialogVisible = true;
			this.$API.shop.goodsActivity.info({ id:data.id }).then(res => {
				if (res.code === 0) {
					let resData = res.data;
					resData.start_time = dayjs.unix(resData.start_time).format("YYYY-MM-DD HH:mm:ss");
					resData.end_time = dayjs.unix(resData.end_time).format("YYYY-MM-DD HH:mm:ss");
					this.form = resData;
				}
				else {
					this.$message.error(res.message);
				}
			})
		},
		submitData() {
			let submitData = Object.assign({}, this.form);
			submitData.start_time = dayjs(submitData.start_time).unix();
			submitData.end_time = dayjs(submitData.end_time).unix();
			let meName = '';
			if (submitData.id > 0) {
				meName = 'update';
			}
			else {
				meName = 'add';
			}
			this.$API.shop.goodsActivity[meName](submitData).then(res => {
				if (res.code === 0) {
					this.$message.success('提交成功');
					this.dialog.dialogVisible = false;
					this.$emit('success', submitData);
				}
				else {
					this.$message.error(res.message);
				}
			});
		}
	}
}
</script>

<style scoped>

</style>
